package net.tribal.comic.entities

import java.math.*

class AlterPage {

	//	Attributes
	String 	title
	String 	description
	String 	nameFile
	
	boolean moderationRequest 	= false
	boolean rateable 			= true
	boolean commentable 		= true
	boolean disabled 			= false
	
	Date 	dateCreated
	Date 	lastUpdated

	
	//	Not Persistence Attributes
	static transients = ['rate', 'pathFile']	
	
	
	//	DB Relations & Constraits
	static belongsTo = 	[page:		Page, 
	                   	 createdBy:	User]
						 
	static hasMany =	[coments:	Comment,
	                	 ratings:	Rate,
	                 	 tags:		Tag]
	
	
	//	DB Constraits
	static 	constraints = {
		title(blank:false, unique:true, maxSize:50)
		description(nullable:true, maxSize:1000)
		nameFile(blank:true, nullable:true)
	}
	
	
	//	Other Methods
	String getRate() {
		//	We calculated the average scores
		Float p = 0
		if(ratings){
			ratings.each(){ p += it.rate }
			p = p / ratings.size()
		}
		return p
	}
	
	String getPathFile() {
		def p = ""
		if (nameFile == null || nameFile == "") {
			nameFile = "noneComic.png"
			return "../images/"
		}
		return "../uploads/comic/"
	}
}